package com.jiyun.controller.goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    private HttpServletRequest request;

    @PostMapping("/native")
    public String nativeUpload(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws IOException {
        String filename = file.getOriginalFilename();
        String realPath = request.getRealPath("photo");
        file.transferTo(new File(realPath+"/"+filename));

        return "/photo/"+filename;

    }

//    @Autowired
//    private OSSClient ossClient;
//
//    @PostMapping("/oss")
//    public String ossUpload(@RequestParam("file") MultipartFile file,String folder){
//        String bucketName = "qing-cheng";
//        String fileName= folder+"/"+ UUID.randomUUID()+"_"+file.getOriginalFilename();
//        try {
//            ossClient.putObject(bucketName, fileName, file.getInputStream());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return "http://"+bucketName+"."+ ossClient.getEndpoint().toString().replace("http://","") +"/"+fileName;
//    }


}
